package com.metaera.app.ui.fragments;

import android.os.Bundle;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.metaera.app.R;
import com.metaera.app.controller.core.IPaginationPresenter;
import com.metaera.app.controller.core.SimplePaginationPresenter;
import com.metaera.app.controller.view.core.IPaginationView;
import com.metaera.app.data.DataRepository;
import com.metaera.app.data.model.ActivityListResponse;
import com.metaera.app.data.model.BasicListResponse;
import com.metaera.app.data.network.ApiParam;
import com.metaera.app.ui.adapters.ActivityListAdapter;
import com.metaera.app.ui.adapters.SimpleFragmentStatePagerAdapter;
import com.metaera.app.ui.adapters.navigator.TabCommonNavigatorAdapter;
import com.metaera.app.ui.core.BaseRequestFragment;
import com.metaera.app.ui.core.BaseRequestListFragment;


import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;


import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;
import io.reactivex.Observable;

/**
 * 首页活动条目
 */
public class ActivityListFragment extends BaseRequestFragment {

    @Override
    protected int getLayoutId() {
        return R.layout.activity_activity_list;
    }

    @Override
    protected void initialize(Bundle savedInstanceState) {
        // find view
        ViewPager viewPager = findViewById(R.id.view_pager);
        MagicIndicator magicIndicator = findViewById(R.id.magic_indicator);

        // init
        List<Fragment> fragmentList = new ArrayList<>();
        List<String> titleList = Arrays.asList(getResources().getStringArray(R.array.activity_types));
        CommonNavigator commonNavigator = new CommonNavigator(getContext());
        commonNavigator.setAdapter(new TabCommonNavigatorAdapter(viewPager, titleList));

        for (int i = 0; i < titleList.size(); i++) {
            // 活动类型：1-峰会；2-系列活动；3-沙龙/论坛；4-培训；5-线上活动
            fragmentList.add(ListFragment.newInstance(i));
        }

        magicIndicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(magicIndicator, viewPager);
        viewPager.setAdapter(new SimpleFragmentStatePagerAdapter(getChildFragmentManager(), fragmentList));
    }


    public static class ListFragment extends BaseRequestListFragment<ActivityListResponse> {

        public static Fragment newInstance(int type) {
            Bundle bundle = new Bundle();
            bundle.putInt("TYPE", type);
            ActivityListFragment.ListFragment fragment = new ActivityListFragment.ListFragment();
            fragment.setArguments(bundle);
            return fragment;
        }

        @Override
        protected BaseQuickAdapter<ActivityListResponse, BaseViewHolder> createAdapter(RecyclerView recyclerView) {
            return new ActivityListAdapter();
        }

        @Override
        protected IPaginationPresenter<IPaginationView<ActivityListResponse>, ActivityListResponse> createPresenter() {
            Bundle arguments = getArguments();
            int type = Objects.requireNonNull(arguments).getInt("TYPE");
            return new SimplePaginationPresenter<ActivityListResponse>() {
                @Override
                protected Observable<BasicListResponse<ActivityListResponse>> execute() {
                    return DataRepository.getInstance().getActivityList(type, mPage, ApiParam.PAGE_SIZE);
                }
            };
        }
    }

}
